﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class Scout : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // security part
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");

            OnlineUser ou = (OnlineUser)Session["User"];
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.Scout_PageTitle;
            this.Title = Resources.Resource.ScoutPageTitle + " - " + GND.ReturnAppTitle();

            if (ou.TeamId == "Null")
                Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");

            if (Request.QueryString["ConfirmNote"] != null)
            {
                this.Message.Text = Request.QueryString["ConfirmNote"].ToString();
            }

            this.MessagePanel.Visible = (this.Message.Text.Length > 0);

            if (!ou.IsPremium)
                Response.Redirect("~/GeneralMessage.aspx?MessageString=gfkkjFSDei3w7lzTD");

            // end of security part
            ////////////////////////////////////////////////////////////////////
            this.PlayerScoutDataSourse.UpdateParameters.Add("UserId", ou.UserId);
            this.CoachesDataSourse.UpdateParameters.Add("UserId", ou.UserId);
            if (!IsPostBack)
            {
                this.PlayerScoutDataSourse.SelectParameters.Add("UserId", ou.UserId);
                this.PlayerScoutDataSourse.SelectParameters.Add("Language", GND.GetUserLanguage());
                this.ShowPlayerAttributeValuesSqlDataSource.SelectParameters.Add("UserId", ou.UserId);
                this.PlayerReportsDataSourse.SelectParameters.Add("UserId", ou.UserId);
                this.CoachesDataSourse.SelectParameters.Add("UserId", ou.UserId);
                this.CoachReportsDataSource.SelectParameters.Add("UserId", ou.UserId);
            }
            else
            {
                this.Message.Text = "";

            }

        }


        protected void PlayersRowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
                GND.ExecNivdalSQL("ReportId", "Nivdal_ScoutMarkScotReportDeletedByUser", e.CommandArgument);
                Response.Redirect("~/Scout.aspx?ConfirmNote=" + Resources.Resource.Scout_ReportDeletedMessage);
            }

        }

        protected string DeleteOnClientClick()
        {
            return ("return confirm('" + Resources.Resource.Global_DeleteThisReport + "?')");
        }

        protected string ReleaseScoutConfirm()
        {
            return ("return confirm('" + Resources.Resource.Scout_DeactivateScoutConfirmText + "')");
        }

        protected void DeactivateScout(object sender, FormViewCommandEventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            if (e.CommandName == "DeactivatePlayer")
            {
                GND.ExecNivdalSQL("UserId", "Nivdal_ScoutDeactivatePlayersScout", ou.UserId);
                Response.Redirect("~/Scout.aspx?ConfirmNote=" + Resources.Resource.Scout_ResponceToDeactivatePlayersScout);
            }
            else if (e.CommandName == "DeactivateCoach")
            {
                GND.ExecNivdalSQL("UserId", "Nivdal_ScoutDeactivateCoachScout", ou.UserId);
                Response.Redirect("~/Scout.aspx?ConfirmNote=" + Resources.Resource.Scout_ResponceToDeactivateCoachesScout);
            }

        }
        protected void RefreshPlayersGrid(object sender, EventArgs e)
        {
            if (this.PlayerScoutFormView.CurrentMode == FormViewMode.ReadOnly)
            //Response.Redirect("~/Scout.aspx");
            this.PlayerReportsGridView.DataBind();

        }

        protected void RefreashCoachesGrid(object sender, EventArgs e)
        {
            if (this.ScoutAssignmentFormView.CurrentMode == FormViewMode.ReadOnly)
                this.ScoutReportsGridView.DataBind();
        }

        protected override void InitializeCulture()
        {

            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected void DeleteAllPlayers(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            string ReportId;
            foreach (GridViewRow row in PlayerReportsGridView.Rows)
            {
                if (row.Visible)
                {
                    ReportId = (row.FindControl("ReportIdHiddenField") as HiddenField).Value;
                    GND.ExecNivdalSQL("ReportId", "Nivdal_ScoutMarkScotReportDeletedByUser", ReportId);
                }
            }
            PlayerReportsDataSourse.DataBind();
            PlayerReportsGridView.DataBind();
        }

        protected void DeleteSelectedPlayers(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            string ReportId;
            bool DeleteChecked;
            foreach (GridViewRow row in PlayerReportsGridView.Rows)
            {
                DeleteChecked = (row.FindControl("ToDeleteCheckBox") as CheckBox).Checked;
                if (DeleteChecked)
                {
                    ReportId = (row.FindControl("ReportIdHiddenField") as HiddenField).Value;
                    GND.ExecNivdalSQL("ReportId", "Nivdal_ScoutMarkScotReportDeletedByUser", ReportId);
                }
            }
            PlayerReportsDataSourse.DataBind();
            PlayerReportsGridView.DataBind();
        }



        protected void DeleteAllCoaches(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            string ReportId;
            foreach (GridViewRow row in ScoutReportsGridView.Rows)
            {
                if (row.Visible)
                {
                    ReportId = (row.FindControl("ReportIdHiddenField") as HiddenField).Value;
                    GND.ExecNivdalSQL("ReportId", "Nivdal_ScoutMarkScotReportDeletedByUser", ReportId);
                }
            }
            CoachReportsDataSource.DataBind();
            ScoutReportsGridView.DataBind();
        }

        protected void DeleteSelectedCoaches(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            string ReportId;
            bool DeleteChecked;
            foreach (GridViewRow row in ScoutReportsGridView.Rows)
            {
                DeleteChecked = (row.FindControl("ToDeleteCheckBox") as CheckBox).Checked;
                if (DeleteChecked)
                {
                    ReportId = (row.FindControl("ReportIdHiddenField") as HiddenField).Value;
                    GND.ExecNivdalSQL("ReportId", "Nivdal_ScoutMarkScotReportDeletedByUser", ReportId);
                }
            }
            CoachReportsDataSource.DataBind();
            ScoutReportsGridView.DataBind();
        }

    }
}